﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Types;

namespace Client
{
    public partial class SearchForm : Form
    {
        private string username { get; set; }
        private List<object> searchResultList = new List<object>();

        public delegate void UpdateSearchListDelegate(List<object> searchResultList);

        public SearchForm(string currentUsername)
        {
            InitializeComponent();
            username = currentUsername;
            ClientManager.Instance.updateSearchList += new ClientManager.UpdateSearchList(this.UpdateSearchList);
        }

        private void UpdateSearchList(List<object> searchResultList)
        {
            if (InvokeRequired)
            {
                Invoke(new UpdateSearchListDelegate(this.UpdateSearchList), searchResultList);
            }
            else
            {
                foreach (var result in searchResultList)
                {
                    if (result is QuestionType)
                    {
                        QuestionType question = result as QuestionType;
                        searchResultListBox.Items.Add(question.summary);
                    }
                    else if (result is AnswerType)
                    {
                        AnswerType answer = result as AnswerType;
                        searchResultListBox.Items.Add(answer.summary);
                    }
                }
                this.searchResultList = searchResultList;
            }
        }

        private void noDatesRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (noDatesRadioButton.Checked)
            {
                startDateTimePicker.Enabled = false;
                endDateTimePicker.Enabled = false;
            }
        }

        private void betweenDatesRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (betweenDatesRadioButton.Checked)
            {
                startDateTimePicker.Enabled = true;
                endDateTimePicker.Enabled = true;
            }
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            searchResultListBox.Items.Clear();
            if (searchCriteriaTextBox.Text.Equals(""))
	        {
                MessageBox.Show("Please insert search criteria", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
	        }
            if (noDatesRadioButton.Checked)
            {
                ClientManager.Instance.Search(searchCriteriaTextBox.Text, username);

            }
            else if (betweenDatesRadioButton.Checked)
            {
                ClientManager.Instance.SearchBetweenDates(searchCriteriaTextBox.Text, username, startDateTimePicker.Value, endDateTimePicker.Value);
            }
        }

        private void searchResultListBox_MouseClick(object sender, MouseEventArgs e)
        {
            object result = searchResultList[searchResultListBox.SelectedIndex];
            if (result is AnswerType)
            {
                AnswerType answer = result as AnswerType;
                SearchResultAnswerForm searchResultAnswerForm = new SearchResultAnswerForm(answer);
                ClientManager.Instance.GetFileContent(answer);
                ClientManager.Instance.GetCourseName(answer);
                ClientManager.Instance.GetQuestionSummary(answer);
                searchResultAnswerForm.ShowDialog();
            }
            else if(result is QuestionType)
            {
                QuestionType question = result as QuestionType;
                SearchResultQuestionForm searchResultQuestionForm = new SearchResultQuestionForm(question);
                ClientManager.Instance.GetFileContent(question);
                ClientManager.Instance.GetCourseName(question);
                searchResultQuestionForm.ShowDialog();
            }
        }
    }
}
